import cv2,os
from PIL import Image
import numpy as np


def resize_and_replace(image_path, target_width, target_height):
    try:
        # 打开图片
        with Image.open(image_path) as img:
            # 调整图片大小
            resized_img = img.resize((target_width, target_height), Image.Resampling.LANCZOS)
            # 覆盖保存到原路径
            resized_img.save(image_path)
            print(f"图片已成功调整大小并覆盖原文件: {image_path}")
    except Exception as e:
        print(f"调整图片大小时发生错误: {e}")

def get_file_paths(directory):
    file_paths = []
    for root, dirs, files in os.walk(directory):
        for file in files:
            temp=os.path.join(root, file)
            file_paths.append(temp.replace('//','/'))
    return file_paths

def resize_all(dir):
    paths=get_file_paths(dir)
    for path in paths:
        file=os.path.basename(path)
        if file=='1.jpg':
            resize_and_replace(path,1200,1200)
        else:
            if 'Thumbs' in path:
                os.remove(path)
                continue
            resize_and_replace(path,1340,1785)

dir='D:/Desk/半托图片/0321/男装/完成/白色'
resize_all(dir)